package cn.wolfcode.rbac.controller;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.qo.EmployeeQo;
import cn.wolfcode.rbac.service.DepartService;
import cn.wolfcode.rbac.service.EmployeeService;
import cn.wolfcode.rbac.service.RoleService;
import cn.wolfcode.rbac.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author xiahuihui
 * #Description EmployeeController
 * #Date: 2022/7/2 09:21
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartService departService;

    @Autowired
    private RoleService roleService;

    //查询员工
    @RequestMapping("/list")
    @RequiredPermission({"员工列表","employee:list"})
    public String list(Model model,@ModelAttribute("qo") EmployeeQo qo){
        PageInfo<Employee> pageResult = employeeService.findByPage(qo);
        model.addAttribute("pageResult",pageResult);
        List<Department> departmentAll = departService.findDepartmentAll();
        model.addAttribute("departmentAll",departmentAll);
        return "employee/list";
    }

    //编辑员工
    @RequestMapping("/input")
    @RequiredPermission({"员工编辑","employee:input"})
    public String addEmployee(Model model,Long id){
        if(id != null){
            //根据id查询到对应的员工信息然后进行回显
            Employee employee = employeeService.fingById(id);
            model.addAttribute("employee",employee);
        }
        //查询所有部门信息
        List<Department> departments = departService.findDepartmentAll();
        model.addAttribute("departments",departments);
        //查询所有角色信息
        List<Role> roles = roleService.findRoleAll();
        model.addAttribute("roles",roles);
        return "employee/input";
    }

    //员工编辑页面
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission({"员工保存或更新","employee:saveOrUpdate"})
    public String saveEmployee(Employee employee,Long[] roleIds){
    //将获取到的数据保存到数据库中
        employeeService.save(employee,roleIds);

    //数据保存后，跳转到列表页面
        return "redirect:list";
    }

    //删除员工
    @RequestMapping("/delete")
    @RequiredPermission({"员工删除","employee:delete"})
    public String deleteEmployee(Long id){
        employeeService.deleteEmployee(id);
        return "redirect:list";
    }

    //导入
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file){
        employeeService.importXls(file);
        return "redirect:list";
    }

    //导出
    @RequestMapping("/exportXls")
    public void  exportXls(HttpServletResponse response){
        //生成一个Excel文件，发给浏览器，供用户下载
        Workbook workbook = employeeService.exportXls();

        //设置响应头,告诉浏览器下载响应的内容,Content-Disposition是一个固定值
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");

        try {
            //获取响应输出流
            ServletOutputStream outputStream = response.getOutputStream();

            //将workbook对象通过响应输出流写出
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
